package cn.hr.services.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import cn.hr.dao.CommonDao;
import cn.hr.entity.College;
import cn.hr.entity.Specialty;
import cn.hr.services.ISpecialtyService;
import cn.hr.vo.PageAttribute;

/**
 * 专业业务逻辑类
 * 
 * @author 戚进堂
 * 
 */
@Service("specialtyServiceImpl")
public class SpecialtyServiceImpl implements ISpecialtyService {

	@Resource(name = "commonDao")
	public CommonDao commonDao;

	public CommonDao getCommonDao() {
		return commonDao;
	}

	public void setCommonDao(CommonDao commonDao) {
		this.commonDao = commonDao;
	}

	public String addSpecialty(int collegeId, String specialtyName) {
		String message = "";
		if (specialtyName.length() == 0)
			return message = "专业名称不能为空!";
		else {
			try {
				Criterion[] criterions = { Restrictions.eq("specialtyName",
						specialtyName) };
				List list = commonDao.find(Specialty.class, criterions);
				if (list.size() != 0)
					return message = "专业名称已经存在!";
				Specialty specialty = new Specialty();
				College college = (College) commonDao.get(College.class,
						collegeId);
				specialty.setCollege(college);
				specialty.setSpecialtyName(specialtyName);
				commonDao.save(specialty);
				message = "添加专业成功!";
			} catch (Exception e) {
				e.printStackTrace();
				message = "添加专业成功!";
			}
		}
		return message;
	}

	public String deleteSpecialty(int specialtyId) {
		String message = "";
		try {
			commonDao.delete(Specialty.class, specialtyId);
			message = "删除专业成功!";
		} catch (Exception e) {
			e.printStackTrace();
			message = "删除专业失败!";
		}
		return message;
	}

	public String modifySpecialty(int specialtyId, int collegeId,
			String specialtyName) {
		String message = "";
		try {
			Specialty specialty = (Specialty) commonDao.get(Specialty.class,
					specialtyId);
			College college = (College) commonDao.get(College.class, collegeId);
			specialty.setCollege(college);
			specialty.setSpecialtyName(specialtyName);
			commonDao.save(specialty);
			message = "修改专业成功!";
		} catch (Exception e) {
			e.printStackTrace();
			message = "修改专业失败!";
		}
		return message;
	}

	public PageAttribute findAllSpecialty(int page, int size) {
		PageAttribute pageAttribute = new PageAttribute ();
		try {
			pageAttribute.setPageList(commonDao.find(Specialty.class,page,size));
			pageAttribute.setAllCount(commonDao.count(Specialty.class));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return pageAttribute;
	}
}
